package edu.geo.containers.geom.impl;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.postgis.PGgeometry;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/**
 * 
 * @author alejandro.sanchez
 *
 */
public class GeometryFactory implements Serializable{
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8964026698020601751L;
	
	private static final Log LOGGER = LogFactory
			.getLog(GeometryFactory.class);
	
	private static final com.vividsolutions.jts.geom.GeometryFactory _GEO_FACTORY = new com.vividsolutions.jts.geom.GeometryFactory();
	private static final WKTReader _WKT_READER = new WKTReader();

        ///////////////////////////////////// POINT INSTANCES ///////////////////////////////////////
	public static Point createPoint(double pLongitude, double pLatitude)
	{
		return new Point( _GEO_FACTORY.getCoordinateSequenceFactory().create(new Coordinate[]{new Coordinate(pLongitude, pLatitude)}), _GEO_FACTORY );
	}
	
	public static Point createPoint(int x, int y)
	{
		return new Point( _GEO_FACTORY.getCoordinateSequenceFactory().create(new Coordinate[]{new Coordinate(x, y)}), _GEO_FACTORY );
	}

     public static Point createPointFromWKT(String wktPoint) throws ParseException
	{
		com.vividsolutions.jts.geom.Point point = (com.vividsolutions.jts.geom.Point) _WKT_READER.read(wktPoint);
		return createPoint(point.getX(), point.getY());
	}
	
	public static Point createPoint( PGgeometry pgGeom )
	{
		org.postgis.Point geom = pgGeom.getGeometry().getFirstPoint();				
		return new Point( _GEO_FACTORY.getCoordinateSequenceFactory().create(new Coordinate[]{new Coordinate(geom.x, geom.y)}), _GEO_FACTORY );
	}

    public static Point createPoint(Coordinate[] coordinate)
	{
    	LOGGER.info(coordinate.length);
    	if (coordinate.length > 1)
    	{
    		Coordinate coord[] = new Coordinate[1];
    		coord[0] = coordinate[0];    		
    		return new Point( _GEO_FACTORY.getCoordinateSequenceFactory().create(coord), _GEO_FACTORY );
    	}
		return new Point( _GEO_FACTORY.getCoordinateSequenceFactory().create(coordinate), _GEO_FACTORY );
	}
        
    public static Point createPoint(com.vividsolutions.jts.geom.Point aPoint)
	{
		return new Point( _GEO_FACTORY.getCoordinateSequenceFactory().create(aPoint.getCoordinates()), _GEO_FACTORY );
	}
        
    //////////////////// LINE SEGMENT INSTANCES ////////////////////////////    
    public static LineSegment createLineSegment( Point beginNode, Point endNode )
    {
    	return new LineSegment( beginNode, endNode, _GEO_FACTORY );
    }
    
    public static LineSegment createLineSegment( int id, Point beginNode, Point endNode )
    {
    	return new LineSegment( id, beginNode, endNode, _GEO_FACTORY );
    }
    
    public static Polygon createPolygon( int id, com.vividsolutions.jts.geom.Polygon jtsPolygon)
    {
    	return new Polygon( id, jtsPolygon );    	
    }
    public static Polygon createPolygon(com.vividsolutions.jts.geom.Polygon jtsPolygon)
    {
    	return new Polygon( jtsPolygon );    	
    }
    
    /*
    public static LineSegment createLineSegment( int id )
    {
    	return new LineSegment( id, _GEO_FACTORY );
    }
    */
    
}
